package com.toweave.starter.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Spinner;

import com.toweave.starter.R;
import com.toweave.starter.adapter.CityBaseAdapter;
import com.toweave.starter.beans.CityBean;
import com.toweave.starter.components.ToastUtil;

import java.util.Arrays;
import java.util.List;

public class BaseAdapterActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {

  private List<CityBean> cityList;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_base_adapter);
    // 获取数据
    cityList = Arrays.asList(CityBean.getCityBeans());
    Spinner spinner_base_dialog_list = findViewById(R.id.spinner_base_dialog_list);
    // 构建适配器
    CityBaseAdapter adapter = new CityBaseAdapter(this, cityList);
    spinner_base_dialog_list.setAdapter(adapter);

    // 设置默认选择第一项
    spinner_base_dialog_list.setSelection(0);
    // 设置监听事件
    spinner_base_dialog_list.setOnItemSelectedListener(this);
  }

  @Override
  public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    CityBean cityBean = cityList.get(position);
    new ToastUtil(BaseAdapterActivity.this, cityBean.toString());
  }

  @Override
  public void onNothingSelected(AdapterView<?> parent) {

  }
}